package com.ams.thread.lesson7;

import cn.hutool.core.thread.ThreadUtil;
import com.ams.thread.lesson5.MyMutexLock;
import lombok.extern.slf4j.Slf4j;

/**
 * @author： 乐哥聊编程(全平台同号)
 */
@Slf4j
public class Example19 {
private static ReentrantMutexLock reentrantMutexLock = new ReentrantMutexLock();

    public static void main(String[] args) {
        new Thread(new Thread1()).start();
        new Thread(new Thread2()).start();
    }
    static class Thread1 implements Runnable {

        @Override
        public void run() {
            reentrantMutexLock.lock();
            ThreadUtil.sleep(1000);
            log.info("Thread1 获取到锁");
            ThreadUtil.sleep(1000);
            reentrantMutexLock.lock();
            ThreadUtil.sleep(1000);
            log.info("Thread1 再次获取到锁");
            ThreadUtil.sleep(1000);
            reentrantMutexLock.unlock();
            ThreadUtil.sleep(1000);
            log.info("Thread1 释放锁");
            reentrantMutexLock.unlock();
            ThreadUtil.sleep(1000);
            log.info("Thread1 释放所有锁");
        }
    }
    static class Thread2 implements Runnable {

        @Override
        public void run() {
            reentrantMutexLock.lock();
            ThreadUtil.sleep(1000);
            log.info("Thread2 获取到锁");
            reentrantMutexLock.lock();
            ThreadUtil.sleep(1000);
            log.info("Thread2 再次获取到锁");
            ThreadUtil.sleep(5000);
            ThreadUtil.sleep(1000);
            reentrantMutexLock.unlock();
            log.info("Thread2 释放锁");
            reentrantMutexLock.unlock();
            ThreadUtil.sleep(1000);
            log.info("Thread2 释放所有锁");
        }
    }
}
